import { _decorator, find, Node } from 'cc';
import { BaseLayer } from './BaseLayer';
import { DataMgr } from '../manager/DataMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { ENUM_AUDIO_CLIP, ENUM_UI_LAYER } from '../Enum';
import { Ins } from '../Ins';
import { SdkMgr } from '../manager/SdkMgr';
const { ccclass, property } = _decorator;

@ccclass('SettingLayer')
export class SettingLayer extends BaseLayer {
    btn_close: Node = null
    btn_music: Node = null
    btn_sound: Node = null

    onLoad() {
        this.btn_close = find('panel/header/btn_close', this.node)
        this.btn_music = find('panel/main/buttons/btn_music', this.node)
        this.btn_sound = find('panel/main/buttons/btn_sound', this.node)
        this.btn_music.on('click', this.onMusicClick, this)
        this.btn_sound.on('click', this.onSoundClick, this)
        this.btn_close.on('click', this.onCloseClick, this)
    }

    onDestroy() {
        this.btn_music.off('click', this.onMusicClick, this)
        this.btn_sound.off('click', this.onSoundClick, this)
        this.btn_close.off('click', this.onCloseClick, this)
    }

    onEnable() {
        this.rendor()
        SdkMgr.ins.toggleBannerAd(true)
    }

    onDisable() {
        super.onDisable()
        SdkMgr.ins.toggleBannerAd(false)
    }

    rendor() {
        this.btn_music.getChildByName('on').active = DataMgr.ins.isMusic
        this.btn_music.getChildByName('off').active = !DataMgr.ins.isMusic
        this.btn_sound.getChildByName('on').active = DataMgr.ins.isSound
        this.btn_sound.getChildByName('off').active = !DataMgr.ins.isSound
    }

    onMusicClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataMgr.ins.isMusic = !DataMgr.ins.isMusic
        DataMgr.ins.isMusic ? AudioMgr.ins.playMusic() : AudioMgr.ins.stopMusic()
        this.rendor()
        DataMgr.ins.save()
    }

    onSoundClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataMgr.ins.isSound = !DataMgr.ins.isSound
        this.rendor()
        DataMgr.ins.save()
    }

    async onCloseClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        await Ins.FadeMgr.fadeIn()
        Ins.UIMgr.toggle(ENUM_UI_LAYER.SETTING, false)
        Ins.UIMgr.toggle(ENUM_UI_LAYER.MENU)
    }
}


